import time
from typing import Union, Tuple, Any

import jwt
from pydantic import BaseModel


def generate_jwt_token(payload: Union[BaseModel, dict], secret: str, algorithm: str) -> str:
    if isinstance(payload, BaseModel):
        payload = payload.dict()
    token = jwt.encode(payload=payload,
                       key=secret,
                       algorithm=algorithm)
    return token


def decode_jwt_token(token: str, secret: str, algorithms: str)->Tuple[bool, Any]:
    try:
        payload = jwt.decode(jwt=token,
                             key=secret,
                             algorithms=algorithms)
    except jwt.PyJWTError:
        return False, ""
    else:
        exp = int(payload.get("exp") or 3600)
        if time.time() > exp:
            return False, ""
        return True, payload
